import { TemplateOptions } from './types';
import ConfigAPI from '@/api/message/config.api';
export default function useEdit() {
  const formData = ref<TemplateOptions>({
    id: '',
    templateName: '',
    configType: '',
    templateContent: '',
    targetUserRule: [],
    sendTimeRule: '',
    enabled: true,
    createdTime: '',
    updatedTime: '',
    remark: '',
  });
  const submitFormData = async (formDataVal?: any) => {
    formDataVal = formDataVal || formData.value;
    let api: any = null;
    if (!formDataVal.id) {
      api = ConfigAPI.addTemplate(formDataVal);
    } else {
      api = ConfigAPI.editTemplate(formDataVal);
    }
    const ret: any = await api;
    return ret;
  };
  const deleteFormData = async (id: string) => {
    const ret: any = await ConfigAPI.delTemplate(id);
    return ret;
  };
  const resetFormData = () => {
    formData.value = {
      id: '',
      templateName: '',
      configType: '',
      templateContent: '',
      targetUserRule: [],
      sendTimeRule: '',
      enabled: true,
      createdTime: '',
      updatedTime: '',
      remark: '',
    };
  };
  return {
    formData,
    submitFormData,
    resetFormData,
    deleteFormData,
  };
}
